package com.company;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字母排序
 *
 * @author jianf
 * @date 2021年09月10 21:19
 */
public class StringSort {

    public int getPos(List<String> deCodes,int legth,String str){
        List<String>  parm = new ArrayList<>();
        List<String> list = getList(deCodes,legth,parm);//.stream()
//                .sorted((x,y)->x.compareTo(y)).collect(Collectors.toList());
        System.out.println("可用字母："+deCodes.toString());
        System.out.println("拼成的字符串最大长度："+legth);
        System.out.println("拼成的字符串："+list.toString());
        return list.indexOf(str)+1;
    }

    /**
     * 生成字符串数组，递归方生成
     * @param deCodes 源字符串
     * @param legth 新串长度
     * @param curList 上一层的结果
     * @return
     */
    public List<String> getList(List<String> deCodes,int legth,List<String> curList){

        List<String> result = new ArrayList<>(1);
        //字符串长度为1时
        if(legth <= 1){
            curList.addAll(deCodes);
            result.addAll(curList);
        }else{
            // 长于1时，递归到1的结果
            result = getList(deCodes,legth -1,curList);
            List<String>  temp = new ArrayList<>(1);
            //在上一次的基础上，生成本次的结果
            for (int j = 0; j < curList.size(); j++) {
                for (int i = 0; i <deCodes.size() ; i++) {
                    temp.add(curList.get(j)+deCodes.get(i));
                }
            }
            curList.clear();
            curList.addAll(temp);
            result.addAll(temp);
        }
        return result;
    }

    public static void main(String[] args) {
        List<String> code = new ArrayList<>();
        code = Arrays.asList("A","B");

        StringSort c = new StringSort();
        System.out.println("结果是： "+ c.getPos(code,3,"ABA"));
    }
}
